package org.nnxy.foodboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.nnxy.foodboot.common.Result;
import org.nnxy.foodboot.entity.Recipe;
import org.nnxy.foodboot.service.UserCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户收藏控制器
 */
@RestController
@RequestMapping("/collection")
public class UserCollectionController {
    
    @Autowired
    private UserCollectionService userCollectionService;
    
    /**
     * 添加收藏
     * @param userId 用户ID
     * @param recipeId 食谱ID
     * @param notes 收藏备注
     * @return 收藏结果
     */
    @PostMapping("/add")
    public Result<Boolean> addCollection(
            @RequestParam Long userId,
            @RequestParam Long recipeId,
            @RequestParam(required = false) String notes) {
        
        boolean result = userCollectionService.addCollection(userId, recipeId, notes);
        return Result.success(result);
    }
    
    /**
     * 取消收藏
     * @param userId 用户ID
     * @param recipeId 食谱ID
     * @return 取消结果
     */
    @DeleteMapping("/remove")
    public Result<Boolean> removeCollection(
            @RequestParam Long userId,
            @RequestParam Long recipeId) {
        
        boolean result = userCollectionService.removeCollection(userId, recipeId);
        return Result.success(result);
    }
    
    /**
     * 查询用户收藏列表
     * @param page 页码
     * @param size 每页数量
     * @param userId 用户ID
     * @return 收藏列表
     */
    @GetMapping("/list")
    public Result<IPage<Recipe>> getUserCollections(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam Long userId) {
        
        Page<Recipe> pageParams = new Page<>(page, size);
        IPage<Recipe> result = userCollectionService.pageUserCollections(pageParams, userId);
        return Result.success(result);
    }
    
    /**
     * 检查用户是否收藏
     * @param userId 用户ID
     * @param recipeId 食谱ID
     * @return 是否收藏
     */
    @GetMapping("/check")
    public Result<Boolean> checkCollection(
            @RequestParam Long userId,
            @RequestParam Long recipeId) {
        
        boolean result = userCollectionService.hasCollected(userId, recipeId);
        return Result.success(result);
    }
} 